package utils

// ErrorCode 定义错误码
type ErrorCode int

const (
    Success           ErrorCode = 200
    BadRequest        ErrorCode = 400
    Unauthorized      ErrorCode = 401
    Forbidden         ErrorCode = 403
    NotFound         ErrorCode = 404
    Conflict         ErrorCode = 409
    InternalError    ErrorCode = 500
)

var errorMessages = map[ErrorCode]string{
    Success:        "Success",
    BadRequest:     "Bad Request",
    Unauthorized:   "Unauthorized",
    Forbidden:      "Forbidden",
    NotFound:       "Not Found",
    Conflict:       "Resource Conflict",
    InternalError:  "Internal Server Error",
}

type Error struct {
    Code    ErrorCode `json:"code"`
    Message string    `json:"message"`
}

func NewError(code ErrorCode, message string) *Error {
    if message == "" {
        message = errorMessages[code]
    }
    return &Error{
        Code:    code,
        Message: message,
    }
}
